#include "queueSet.hpp"
#include "taskManager.hpp"
#include "log.h"
#include <iostream>
#include <sstream>

QueueSet::QueueSet(int n):_n(n) {
    std::srand(std::time(nullptr));
    for(int i = 0; i < n; i++) {
        Queue* q = new Queue();
        queues.push_back(q);
    }
}

QueueSet::~QueueSet() {
    for(int i = 0; i < size(); i++) {
        delete queues[i];
    }
}

Task* QueueSet::steal(int id, TaskManager* tm) {
    if(size() <= 1) return nullptr;

    int target = id;
    while (target == id) {
        target = std::rand() % size();
    }

    std::stringstream ss;
    ss << "Thread " << id << ": " << "steal target: " << id;
    LOG(tm->getOutput(), ss.str())
    return get_queue(target)->steal();
}
